<script setup lang="ts">
import { onMounted, ref } from 'vue'
import { ElMessage } from 'element-plus'
import {
  useUserDeleteApi,
  useUserGetAllApi,
  // 假设存在新增用户的 API
  useUserCreateApi,
} from '@/api/user'

onMounted(() => {
  useUserGetAllApi().then((res) => {
    userList.value = res.data.data
  })
})

// 模拟用户数据
const userList = ref([
  {
    id: 1,
    username: 'user1',
    password: 'password1',
    nickname: 'nick1',
    age: 20,
    email: 'user1@example.com',
    role: 'common',
  },
  {
    id: 2,
    username: 'user2',
    password: 'password2',
    nickname: 'nick2',
    age: 25,
    email: 'user2@example.com',
    role: 'common',
  },
])

// 控制修改对话框显示与隐藏
const editDialogVisible = ref(false)
// 当前编辑的用户信息
const currentEditUser = ref({
  id: -1,
  username: '',
  password: '',
  nickname: '',
  age: 0,
  email: '',
  role: 'common',
})

// 控制新增对话框显示与隐藏
const addDialogVisible = ref(false)
// 新增用户信息
const newUser = ref({
  username: '',
  password: '',
  email: '',
  role: 'common',
})

// 提交修改
const handleSubmitEdit = () => {
  const index = userList.value.findIndex(
    (item) => item.email === currentEditUser.value.email,
  )
  if (index !== -1) {
    userList.value[index] = { ...currentEditUser.value }
    ElMessage.success('用户信息修改成功')
    editDialogVisible.value = false
    // 这里可以添加实际的向后端提交修改信息的逻辑
  }
}

// 删除用户
const handleDelete = (id: number) => {
  useUserDeleteApi({ id }).then(() => {
    userList.value = userList.value.filter(
      (item) => item.id !== id,
    )
    ElMessage.success('用户删除成功')
  })
}

// 打开新增对话框
const handleAdd = () => {
  newUser.value = {
    username: '',
    password: '',
    email: '',
    role: 'common',
  }
  addDialogVisible.value = true
}

// 提交新增
const handleSubmitAdd = () => {
  useUserCreateApi(newUser.value)
    .then(() => {
      // 重新获取用户列表
      useUserGetAllApi().then((res) => {
        userList.value = res.data.data
      })
      ElMessage.success('用户新增成功')
      addDialogVisible.value = false
    })
    .catch(() => {
      ElMessage.error('用户新增失败')
    })
}
const search = () => {
  useUserGetAllApi().then((res) => {
    userList.value = res.data.data
    userList.value = userList.value.filter((item) =>
      item.username.includes(searchKeyword.value),
    )
  })
}
const searchKeyword = ref('')
</script>

<template>
  <div>
    <!-- 新增用户按钮 -->
    <el-button type="primary" @click="handleAdd"
      >新增用户</el-button
    >
    <!-- 模糊查询 -->
    <!-- 搜索框和重置按钮 -->
    <div class="search-section">
      <el-input
        v-model="searchKeyword"
        placeholder="请输入用户名进行搜索"
        clearable
      >
        <template #append>
          <el-button @click="search">查询</el-button>
        </template>
      </el-input>
    </div>
    <el-table :data="userList" style="width: 100%">
      <el-table-column prop="id" label="id" />
      <el-table-column prop="username" label="用户名" />
      <!-- <el-table-column prop="nickname" label="昵称" />
      <el-table-column prop="age" label="年龄" /> -->
      <el-table-column prop="email" label="邮箱" />
      <!-- <el-table-column prop="role" label="角色" /> -->
      <el-table-column label="操作">
        <template #default="scope">
          <el-button
            size="small"
            type="danger"
            @click="handleDelete(scope.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 修改对话框 -->
    <el-dialog
      v-model="editDialogVisible"
      title="修改用户信息"
    >
      <el-form :model="currentEditUser" label-width="80px">
        <el-form-item label="用户名">
          <el-input v-model="currentEditUser.username" />
        </el-form-item>
        <el-form-item label="密码">
          <el-input v-model="currentEditUser.password" />
        </el-form-item>
        <el-form-item label="昵称">
          <el-input v-model="currentEditUser.nickname" />
        </el-form-item>
        <el-form-item label="年龄">
          <el-input-number
            v-model="currentEditUser.age"
            :min="0"
          />
        </el-form-item>
        <el-form-item label="邮箱">
          <el-input
            v-model="currentEditUser.email"
            disabled
          />
        </el-form-item>
        <el-form-item label="角色">
          <el-select
            v-model="currentEditUser.role"
            placeholder="请选择角色"
          >
            <el-option label="管理员" value="admin" />
            <el-option label="版主" value="moderator" />
            <el-option label="普通用户" value="common" />
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="editDialogVisible = false"
            >取消</el-button
          >
          <el-button
            type="primary"
            @click="handleSubmitEdit"
            >确定</el-button
          >
        </span>
      </template>
    </el-dialog>

    <!-- 新增对话框 -->
    <el-dialog v-model="addDialogVisible" title="新增用户">
      <el-form :model="newUser" label-width="80px">
        <el-form-item label="用户名">
          <el-input v-model="newUser.username" />
        </el-form-item>
        <el-form-item label="密码">
          <el-input v-model="newUser.password" />
        </el-form-item>
        <el-form-item label="邮箱">
          <el-input v-model="newUser.email" />
        </el-form-item>
        <el-form-item label="角色">
          <el-select
            v-model="newUser.role"
            placeholder="请选择角色"
          >
            <el-option label="管理员" value="admin" />
            <el-option label="版主" value="moderator" />
            <el-option label="普通用户" value="common" />
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="addDialogVisible = false"
            >取消</el-button
          >
          <el-button type="primary" @click="handleSubmitAdd"
            >确定</el-button
          >
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<style lang="scss" scoped>
/* 可添加自定义样式 */
.search-section {
  margin-top: 20px;
  margin-bottom: 20px;
  width: 300px;
}
</style>
